package com.zhanghe.wzq.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhanghe.protocol.serializer.SerializerManager;
import com.zhanghe.protocol.v1.Command;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * MethodHandler
 *
 * @author Clevo
 * @date 2018/12/15
 */
@Slf4j
public class CommandDecoder extends SimpleChannelInboundHandler<String> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception {
        JSONObject json = JSON.parseObject(s);
        Byte command = json.getByte("command");
        Object obj = SerializerManager.getDefault().deserialize(Command.getCommandClass(command),s.getBytes());
        System.out.println(obj);
        channelHandlerContext.fireChannelRead(obj);
    }
}
